package top.csh215.cmd2.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.csh215.cmd2.entity.Permission;
import top.csh215.cmd2.entity.Role;
import top.csh215.cmd2.entity.User;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    
    @Select("SELECT u.* FROM users u WHERE u.username = #{username} AND u.status = 0 AND u.is_delete = 0")
    User selectByUsername(@Param("username") String username);
    
    @Select("SELECT r.* FROM roles r " +
            "JOIN user_roles ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND r.status = 0")
    List<Role> selectRolesByUserId(@Param("userId") Object userId);
    
    @Select("SELECT p.* FROM permissions p " +
            "JOIN role_permissions rp ON p.id = rp.permission_id " +
            "JOIN user_roles ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND p.status = 0")
    List<Permission> selectPermissionsByUserId(@Param("userId") Object userId);
}